using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PartyMemberSystem : MonoBehaviour
{
	[Header("-= Stats =-")]
	public List<ActivePartyMember> ActivePartyMembers = new List<ActivePartyMember>();

	[Header("-= Default PartyMembers =-")]
	public PartyMember Default_NoelleDarkworld;

	public PartyMember Default_SusieDarkworld;

	public PartyMember Default_Susie;

	public PartyMember Default_Ralsei;

	public PartyMember Default_Kris;

	private static PartyMemberSystem instance;

	public static PartyMemberSystem Instance => instance;

	private void Awake()
	{
		if ((Object)(object)instance != (Object)null && (Object)(object)instance != (Object)(object)this)
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
			return;
		}
		instance = this;
		Object.DontDestroyOnLoad((Object)(object)instance);
	}

	private void Start()
	{
		SceneManager.activeSceneChanged += SceneManager_activeSceneChanged;
	}

	public void AddPartyMember(PartyMember Member, int StartingHealth = -1)
	{
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)Member != (Object)null)
		{
			GameObject val = Object.Instantiate<GameObject>(Member.PartyMemberPrefab, ((Component)this).transform);
			if ((Object)(object)val != (Object)null)
			{
				ActivePartyMember activePartyMember = new ActivePartyMember();
				activePartyMember.PartyMemberDescription = Member;
				activePartyMember.PartyMemberTransform = val.transform;
				if (StartingHealth == -1)
				{
					activePartyMember.CurrentHealth = Member.MaximumHealth;
				}
				else
				{
					activePartyMember.CurrentHealth = StartingHealth;
				}
				string partyMemberName = Member.PartyMemberName;
				Scene activeScene = SceneManager.GetActiveScene();
				((Object)val).name = partyMemberName + " " + ((Scene)(ref activeScene)).name;
				ActivePartyMembers.Add(activePartyMember);
				int num = ActivePartyMembers.IndexOf(activePartyMember);
				Susie_Follower component = ((Component)val.transform).GetComponent<Susie_Follower>();
				if ((Object)(object)component != (Object)null)
				{
					activePartyMember.PartyMemberFollowSettings = component;
					component.SusieAnimator.keepAnimatorStateOnDisable = true;
					component.delay = 0.5f + 0.625f * (float)num;
					activePartyMember.PartyMemberTransform.position = ((Component)PlayerManager.Instance).transform.position;
				}
				else
				{
					Debug.LogWarning((object)"Failed to set Party Member Walk Offset! ( No Susie_Follower in Prefab )");
				}
			}
			else
			{
				Debug.LogWarning((object)"Failed to create Party Member! ( No PartyMemberPrefab )");
			}
		}
		else
		{
			Debug.LogWarning((object)"Failed to create Party Member! ( No Member Provided )");
		}
	}

	public void RemovePartyMember(PartyMember SpecificMember = null, bool RemoveSpecificIndex = false, int SpecificMemberIndex = 0)
	{
		if (RemoveSpecificIndex)
		{
			if (SpecificMemberIndex <= ActivePartyMembers.Count)
			{
				Object.Destroy((Object)(object)((Component)ActivePartyMembers[SpecificMemberIndex].PartyMemberTransform).gameObject);
				ActivePartyMembers.RemoveAt(SpecificMemberIndex);
				ResetAllMemberFollowerDelays();
			}
			else
			{
				Debug.LogWarning((object)"Failed to remove Party Member! ( SpecificedMemberIndex is greater than ActivePartyMembers list )");
			}
			return;
		}
		ActivePartyMember activePartyMember = HasMemberInParty(SpecificMember);
		if (activePartyMember != null)
		{
			Object.Destroy((Object)(object)((Component)activePartyMember.PartyMemberTransform).gameObject);
			ActivePartyMembers.Remove(activePartyMember);
			ResetAllMemberFollowerDelays();
		}
		else
		{
			Debug.LogWarning((object)"Failed to remove Party Member! ( SpecificMember is not in ActivePartyMembers )");
		}
	}

	public void RemoveAllPartyMember()
	{
		for (int i = 0; i < ActivePartyMembers.Count; i++)
		{
			Object.Destroy((Object)(object)((Component)ActivePartyMembers[i].PartyMemberTransform).gameObject);
		}
		ActivePartyMembers.Clear();
		ResetAllMemberFollowerDelays();
	}

	public ActivePartyMember HasMemberInParty(PartyMember Member)
	{
		if ((Object)(object)Member == (Object)null)
		{
			return null;
		}
		ActivePartyMember result = null;
		for (int i = 0; i < ActivePartyMembers.Count; i++)
		{
			if ((Object)(object)ActivePartyMembers[i].PartyMemberDescription == (Object)(object)Member)
			{
				result = ActivePartyMembers[i];
			}
		}
		return result;
	}

	public void ResetAllMemberFollowerDelays()
	{
		for (int i = 0; i < ActivePartyMembers.Count; i++)
		{
			if ((Object)(object)ActivePartyMembers[i].PartyMemberFollowSettings != (Object)null)
			{
				ActivePartyMembers[i].PartyMemberFollowSettings.positions.Clear();
				ActivePartyMembers[i].PartyMemberFollowSettings.rotations.Clear();
				ActivePartyMembers[i].PartyMemberFollowSettings.delay = 0.5f + 0.625f * (float)i;
			}
		}
		ResetAllMembersToPlayer();
	}

	public void ResetAllMembersToPlayer()
	{
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		if (ActivePartyMembers.Count <= 0)
		{
			return;
		}
		for (int i = 0; i < ActivePartyMembers.Count; i++)
		{
			if (ActivePartyMembers[i] != null && (Object)(object)ActivePartyMembers[i].PartyMemberFollowSettings != (Object)null)
			{
				ActivePartyMembers[i].PartyMemberFollowSettings.positions.Clear();
				ActivePartyMembers[i].PartyMemberFollowSettings.rotations.Clear();
				ActivePartyMembers[i].PartyMemberTransform.position = ((Component)PlayerManager.Instance).transform.position;
			}
		}
	}

	public Transform GetSpecificMemberTransform(PartyMember Member)
	{
		ActivePartyMember activePartyMember = HasMemberInParty(Member);
		if (activePartyMember != null)
		{
			return activePartyMember.PartyMemberTransform;
		}
		Debug.LogWarning((object)"Failed to get Party Member Transform! ( Member not found in ActivePartyMembers )");
		return null;
	}

	private void SceneManager_activeSceneChanged(Scene arg0, Scene arg1)
	{
	}

	public void SetAllPartyMembersActive(bool ActiveSelf)
	{
		foreach (ActivePartyMember activePartyMember in ActivePartyMembers)
		{
			((Component)activePartyMember.PartyMemberTransform).gameObject.SetActive(ActiveSelf);
		}
	}

	public void SetAllPartyMembersFollowing(bool Following)
	{
		foreach (ActivePartyMember activePartyMember in ActivePartyMembers)
		{
			activePartyMember.PartyMemberFollowSettings.FollowingEnabled = Following;
		}
	}

	public void SetAllPartyMemberStates(Susie_Follower.MemberFollowerStates State)
	{
		foreach (ActivePartyMember activePartyMember in ActivePartyMembers)
		{
			activePartyMember.PartyMemberFollowSettings.currentState = State;
		}
	}
}
